package com.qf.day61.service.impl;

import com.qf.day61.dao.GoodsDAO;
import com.qf.day61.entity.Goods;
import com.qf.day61.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    private final GoodsDAO goodsDAO;

    public GoodsServiceImpl(GoodsDAO goodsDAO) {
        this.goodsDAO = goodsDAO;
    }

    @Override
    public Goods findById(Integer goodId) {
        return goodsDAO.findById(goodId);
    }

    @Override
    public List<Goods> findAllByBrandIdAndCategoryIdAndGoodsName(Integer brandId,
                                                                 Integer categoryId,
                                                                 String goodsName) {
        if (StringUtils.hasLength(goodsName)){
            goodsName = "%" + goodsName + "%";
        }else{
            goodsName = null;
        }
        return goodsDAO.findAllByBrandIdAndCategoryIdAndGoodsName(brandId, categoryId, goodsName);
    }
}
